#!/usr/bin/env python3
import sys, tomlkit

try:
  _, filename = sys.argv
except:
  print("usage: {} CONFIG.toml".format(sys.argv[0]))
  exit(1)

with open(filename, 'rb') as fh:
  config = tomlkit.load(fh)

# Extract the set of features for a particular key from the config,
# using the mapping to rewrite component names to their feature names.
def get_features(config, key, mapping):
  components = set( component['type'] for component in config.get(key, {}).values() )
  components = set( mapping.get(component, component) for component in components )
  return set( f'{key}-{component}' for component in components )

# Mapping of source names to feature names.
source_feature_map = {
  'generator': 'demo_logs',
  'logplex': 'heroku_logs',
  'prometheus_scrape': 'prometheus',
  'prometheus_remote_write': 'prometheus',
}

# Mapping of transform names to feature names.
transform_feature_map = {
  'sampler': 'sample',
  'swimlanes': 'route',
}

# Mapping of sink names to feature names.
sink_feature_map = {
  'gcp_pubsub': 'gcp',
  'gcp_cloud_storage': 'gcp',
  'gcp_stackdriver_logs': 'gcp',
  'gcp_stackdriver_metrics': 'gcp',
  'prometheus_exporter': 'prometheus',
  'prometheus_remote_write': 'prometheus',
  'splunk_hec_logs': 'splunk_hec',
}

features = get_features(config, 'sources', source_feature_map) \
  .union(get_features(config, 'transforms', transform_feature_map)) \
  .union(get_features(config, 'sinks', sink_feature_map))

print(','.join(sorted(features)))
